package com.coldchain.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.util.DateUtil;
import com.coldchain.common.util.FunctionUtils;
import com.coldchain.common.util.ValidateUtil;
import com.coldchain.order.entity.CreditOrder;
import com.coldchain.order.mapper.CreditOrderMapper;
import com.coldchain.order.service.ICreditOrderService;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 企业结算订单 服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-09-25
 */
@Service
@SuppressWarnings("unchecked")
public class CreditOrderServiceImpl extends ServiceImpl<CreditOrderMapper, CreditOrder> implements ICreditOrderService {

	@Override
	public IPage<CreditOrder> queryList(Page<CreditOrder> page,CreditOrder creditOrder){
		QueryWrapper<CreditOrder> wrapper = new QueryWrapper<CreditOrder>();
		if(ValidateUtil.stringEmpty(creditOrder.getUserId())){
			wrapper.eq("user_id", creditOrder.getUserId());
		}
		if(!ValidateUtil.Objectempty(creditOrder.getStatus())){
			wrapper.eq("static", creditOrder.getStatus());
		}
		if(!ValidateUtil.Objectempty(creditOrder.getStarttime())){
			wrapper.gt("createtime", new Date(creditOrder.getStarttime()));
		}
		if(!ValidateUtil.Objectempty(creditOrder.getEndtime())){
			wrapper.lt("createtime", new Date(creditOrder.getEndtime()));
		}
		return super.page(page,wrapper);
	}

	@Override
	public ObjectRestResponse<String> updateStatus(String creditId, String userId) {
		CreditOrder creditOrder = super.getById(creditId);
		if(ValidateUtil.Objectempty(creditOrder)||ValidateUtil.isEquals(creditOrder.getStatus(),0)){
			return FunctionUtils.getSuccessRest("没有找到结算记录或已结算");
		}else if(!ValidateUtil.StringEqualTo(userId, creditOrder.getUserId())){
			return FunctionUtils.getSuccessRest("非法操作");
		}
		creditOrder.setStatus(0);
		if(super.updateById(creditOrder)){
			return FunctionUtils.getSuccessRest("结算成功");
		}
		throw  new BuilderException("结算失败");

	}

	@Override
	public ObjectRestResponse<List<CreditOrder>> queryList(String starttime,String endtime
			,Integer datatype,String user,String status) {
		QueryWrapper<CreditOrder> wrapper = new QueryWrapper<CreditOrder>();
		if(!ValidateUtil.stringEmpty(starttime)){
			wrapper.gt("createtime",starttime);
		}
		if(!ValidateUtil.stringEmpty(endtime)){
			wrapper.lt("createtime",endtime);
		}
		if(!ValidateUtil.Objectempty(datatype)){
			Date[] dates = DateUtil.getDateType(datatype);
			if(dates!=null&&dates.length>1){
				wrapper.between("createtime",dates[0], dates[1]);
			}
		}
		if(ValidateUtil.Objectempty(status)){
			wrapper.eq("static", status);
		}
		if(ValidateUtil.stringEmpty(user)){
			wrapper.eq("user_id",user);
		}
		return FunctionUtils.getSuccessRest("",super.list(wrapper));
	}

}
